/*    
 *     *     作者:     lzf
 *       *    名称:     myping
 *        *     程序应用:  ping命令是向目的主机发送ICMP报文,检验本地主机和远程的目的主机是否连接
 *         *     日期:     2020.04.22
 *          * 
 *          */
 
 /*ICMP必须使用原始套接字进行设计,要手动设置IP的头部和ICMP的头部并行校验*/
/***********主函数*********************************************
 * mypinger.cpp*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <unistd.h>
#include <signal.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h> /* bzero */
#include <netdb.h>
#include <pthread.h>
#define K 1024
#define BUFFERSIZE 72   
typedef struct pingm_pakcet{
	struct timeval tv_begin;     
	struct timeval tv_end;       
	short seq;                   
	int flag;         
}pingm_pakcet;
namespace nyutil
{
    class myPinger
    {
        public :
            myPinger();
            ~myPinger();
            pingm_pakcet *icmp_findpacket(int seq);
            unsigned short icmp_cksum(unsigned char *data, int len);
            struct timeval icmp_tvsub(struct timeval end, struct timeval begin);
            //void icmp_statistics(void);
            void icmp_pack(struct icmp *icmph, int seq, struct timeval *tv,int length);
            int icmp_unpack(unsigned char *buf,int len);
            int icmp_recv(int nsec);
            int icmp_send(int nsec);
            //void icmp_sigint(int signo);
            //void icmp_usage();
            int doPing(char const *ipAddr,int nsec);
        public :
            pingm_pakcet pingpacket[128];
            unsigned char send_buff[BUFFERSIZE];      
            unsigned char recv_buff[2*K];             
            struct sockaddr_in dest;                  
            int rawsock;                          
            pid_t pid;                                
            int alive;                            
            bool isSuccess;                           
            short packet_send;                    
            short packet_recv;                    
            char dest_str[80];
            char res[1024];                        
            struct timeval tv_begin, tv_end, tv_interval;

    };
};
